<?php
	class user
	{
		private $db;
		private $cache;
		public 	$info;
		public 	$is_logged;
		public $id;
		public function __construct()
		{
			//$this->cache = & $GLOBALS['cache'];
			$this->db = & $GLOBALS['db'];
			$this->info = new stdClass;
			$this->is_logged = FALSE;
			$this->if = NULL;
			if(isset($_SESSION['IS_LOGGED'],$_SESSION['LOGGED_USER']) && $_SESSION['IS_LOGGED'])
			{
				$this->is_logged = TRUE;
				$this->info = $_SESSION['LOGGED_USER'];
				$this->id = $this->info->id;
			}
			$_SESSION['LAST_CLICK'] = time();
			//echo time().'<br/>';
		}
		public function logout()
		{
			if( FALSE == $this->is_logged )
			{
				return FALSE;
			}
			setcookie('rememberme', NULL, time()+60*24*60*60, '/', cookie_domain());
			$_COOKIE['rememberme']	= NULL;
			unset($_SESSION['IS_LOGGED']);
			unset($_SESSION['LOGGED_USER']);
			return TRUE;
		}
		public function login($user,$pass,$rememberme = FALSE)
		{
			if(FALSE == is_valid_username($user))
			{
				//echo '<br/>fuck 1<br/>'; 
				return FALSE;
			}
			$user = $this->db->escape($user);
			$pass = $this->db->escape($pass);
			//echo '<br/> you wen ti ...<br/>';
			$this->db->query('SELECT id FROM Users WHERE username="'.$user.'" AND password=md5("'.$pass.'") LIMIT 1');
			if(! $obj = $this->db->fetch_object())
			{
				//echo '<br/>fuck 2<br/>';
				return FALSE;
			}
			//echo '<br/>$obj->id:'.$obj->id.'<br/>';
			$this->info = get_user_by_id($obj->id,TRUE);
			if(! $this->info)
			{
				//echo '<br/>fuck 3<br/>';
				return FALSE;
			}
			//echo '<br/>'.$this->info->city.'<br/>';
			$_SESSION['IS_LOGGED'] = TRUE;
			$_SESSION['LOGGED_USER'] = $this->info;
			$_SESSION['LAST_CLICK'] = time();
			$this->is_logged = TRUE;
			$this->id = $this->info->id;
			$ip = $this->db->escape(ip2long($_SERVER[REMOTE_ADDR]));
			$this->db->query('UPDATE Users SET lastlogin_date="'.time().'", lastlogin_ip="'.$ip.'", lastclick_date="'.time().'" WHERE id="'.$this->id.'" LIMIT 1');
			if(TRUE == $rememberme)
			{
				$tmp	= $this->id.'_'.md5($this->info->username.'#'.$this->info->password.'#'.$_SERVER['HTTP_USER_AGENT']);
				setcookie('rememberme', $tmp, time()+60*24*60*60, '/', cookie_domain());
			//	setcookie('rememberme', $tmp, time()+60*24*60*60, '/');
			}
			setcookie('validuser',1,time()+365*24*60*60,'/',cookie_domain());
			//setcookie('rememberme', $tmp, time()+60*24*60*60, '/');
			//$this->set_lang(LANG);
			return TRUE;
		}
	}
?>